using System;
using UnityEngine;
namespace Super
{
	[Serializable]
	public class GO_Generator
	{
		// Start is called before the first frame update
		public GameObject prefab;
		public Transform parent;
		public int generateNum = 0;
		public Vector3 minPoint;
		public Vector3 maxPoint;
		public Action<GameObject> GenerateCallBack;
		public Action<GOPool> GenerateFinishCallBack;
		public GOPool pool;

		public bool CheckGenerateCondition()
		{
			if (prefab == null)
			{
				LogUtil.Log(LogType.Warn, LogCategory.Default, "prefab����Ϊnull���޷����ɶ���");
				return false;
			}

			if (pool == null)
				pool = new GOPool(prefab, parent);
			return true;
		}

		public void Generate()
		{
			if (!CheckGenerateCondition())
				return;

			pool.ReturnAll();
			CountinueGenerate();
		}

		public void CountinueGenerate()
		{
			if (!CheckGenerateCondition())
				return;

			GameObject go;
			Vector3 offsetPos, pos;
			Vector3 basePos = parent != null ? parent.transform.position : Vector3.zero;

			for (int i = 0; i < generateNum; i++)
			{
				offsetPos = GlobalUtil.RandomVector3(minPoint, maxPoint);
				pos = basePos + offsetPos;

				go = pool.Get();
				go.transform.position = pos;
				go.transform.rotation = Quaternion.identity;

				if (GenerateCallBack != null)
					GenerateCallBack(go);
			}

			if (GenerateFinishCallBack != null)
				GenerateFinishCallBack(pool);
		}
	}
}


